package com.atguigu.serviceedu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.serviceedu.entity.EduTeacher;
import com.atguigu.serviceedu.entity.vo.TeacherQuery;
import com.atguigu.serviceedu.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.awt.print.Pageable;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ychen
 * @since 2021-06-25
 */
@RestController
//@RequestMapping("/serviceedu/edu-teacher")
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
//    注入service
    @Autowired
    private EduTeacherService teacherService;
//    rest风格
//    查询讲师列表

    @GetMapping("findAll")
    @ApiOperation("所有讲师列表")
    public R findAllTeacher(){
       List<EduTeacher> list = teacherService.list(null);
       return R.ok().data("items",list);
    }

//    逻辑删除讲师
    @DeleteMapping("{id}")
    @ApiOperation("逻辑删除讲师")
    public R removeTeacher(@PathVariable String id){
        boolean flag = teacherService.removeById(id);
        return flag?R.ok():R.error();
    }

//    3，分页查询讲师
    @ApiOperation("讲师列表分页")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,@PathVariable long limit){
//current代表当前页   limit 每页显示的个数
//创建page对象
        Page<EduTeacher>pageTeacher = new Page<>(current,limit);
        teacherService.page(pageTeacher,null);
        long total = pageTeacher.getTotal();
        List<EduTeacher> list = pageTeacher.getRecords();
        return R.ok().data("total",total).data("rows",list);
    }

//    多条件组合查询带分页
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    @ApiOperation("多条件组合查询带分页")
    public R pageTeacherCondition(@PathVariable long current, @PathVariable long limit,
                                @RequestBody(required = false) TeacherQuery query){
//        创建page对象
        Page<EduTeacher>pageTeacher = new Page<>(current,limit);
//        构造条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
//        条件的属性
        String name = query.getName();
        Integer level = query.getLevel();
        String begin = query.getBegin();
        String end = query.getEnd();
//        条件插入
        if(StringUtils.hasLength(name)){
            wrapper.like("name",name);
        }
        if(level!=null){
            wrapper.eq("level",level);
        }
        if(StringUtils.hasLength(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if(StringUtils.hasLength(end)){
            wrapper.le("gmt_create",end);
        }

        teacherService.page(pageTeacher, wrapper);
        long total = pageTeacher.getTotal();
        List<EduTeacher> list = pageTeacher.getRecords();
        return R.ok().data("total",total).data("rows",list);

    }

//    添加讲师
    @PostMapping("addTeacher")
    @ApiOperation("添加讲师")
    public R addTeacher(@RequestBody EduTeacher teacher){
        boolean save = teacherService.save(teacher);
        return save?R.ok():R.error();
    }
//    根据id查询讲师
    @ApiOperation("根据id查询讲师")
    @GetMapping("getTeacher/{id}")
    public R getTeacherById(@PathVariable String id){
        EduTeacher teacher = teacherService.getById(id);
        return R.ok().data("teacher",teacher);
    }

    @ApiOperation("讲师修改")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher teacher){
        boolean b = teacherService.updateById(teacher);
        return b?R.ok():R.error();
    }


}

